package com.sf.service;

import com.google.common.base.Preconditions;
import com.sf.dao.RfDeptMapper;
import com.sf.exception.ParamException;
import com.sf.model.RfDept;
import com.sf.param.DeptParam;
import com.sf.util.BeanValidator;
import com.sf.util.LevelUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class RfDeptService {

    @Resource
    private RfDeptMapper rfDeptMapper;

    public void save(DeptParam deptParam) {
        BeanValidator.check(deptParam);
        if (checkExist(deptParam.getParentId(), deptParam.getName(), deptParam.getId())) {
            throw new ParamException("部门名称重复");
        }
        RfDept dept = RfDept.builder().name(deptParam.getName()).parentId(deptParam.getParentId()).seq(deptParam.getSeq())
                .remark(deptParam.getRemark()).build();
        dept.setLevel(LevelUtil.calculateLevel(getLevel(dept.getParentId()), dept.getParentId()));
        dept.setOperator("test-update");
        dept.setOperateIp("192.168.0.1");
        dept.setOperateTime(new Date());
        rfDeptMapper.insertSelective(dept);
    }

    public void update(DeptParam deptParam) {
        BeanValidator.check(deptParam);
        if (checkExist(deptParam.getParentId(), deptParam.getName(), deptParam.getId())) {
            throw new ParamException("部门名称重复");
        }
        RfDept before = rfDeptMapper.selectByPrimaryKey(deptParam.getId());
        RfDept dept = Preconditions.checkNotNull(before, "待更新的部门不存在");
        RfDept after = RfDept.builder().id(deptParam.getId()).name(deptParam.getName()).parentId(deptParam.getParentId())
                .seq(deptParam.getSeq()).remark(deptParam.getRemark()).build();
        after.setLevel(LevelUtil.calculateLevel(getLevel(deptParam.getParentId()), after.getParentId()));
        after.setOperator("test-update");
        after.setOperateIp("192.168.0.1");
        after.setOperateTime(new Date());
        updateWithChild(before, after);
    }

    @Transactional
    public void updateWithChild(RfDept before, RfDept after) {

        String newPreLevel = after.getLevel();
        String oldPreLevel = before.getLevel();
        if (!oldPreLevel.equals(newPreLevel)) {
            List<RfDept> deptList = rfDeptMapper.getChildDeptListByLevel(oldPreLevel);
            if (CollectionUtils.isNotEmpty(deptList)) {
                for (RfDept dept : deptList) {
                    String level = dept.getLevel();
                    if (level.indexOf(oldPreLevel) == 0) {
                        dept.setLevel(level);
                    }
                }
                rfDeptMapper.batchUpdateLevel(deptList);
            }
        }
        rfDeptMapper.updateByPrimaryKey(after);

    }

    private boolean checkExist(Integer prentId, String deptName, Integer deptId) {

        return rfDeptMapper.countByNameAndParentId(prentId, deptName, deptId) > 0;
    }

    private String getLevel(Integer deptId) {
        RfDept dept = rfDeptMapper.selectByPrimaryKey(deptId);
        if (dept == null) {
            return null;
        } else {
            return dept.getLevel();
        }
    }


}
